package com.luozhl;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.demo.common.model._MappingKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import org.junit.After;
import org.junit.BeforeClass;


/**
 * Created by Administrator on 2017/9/14.
 */
public class JFinalModelCase {

    protected static DruidPlugin dp;
    protected static ActiveRecordPlugin activeRecord;

    /**
     * 数据连接地址
     */
    private static String  URL = "jdbc:mysql://localhost:3306/evnet?useUnicode=true&characterEncoding=UTF-8";
    //private static final String  URL = "jdbc:mysql://10.10.120.204:3306/evnet?useUnicode=true&characterEncoding=UTF-8";

    /**
     * 数据库账号
     */
    private static String  USERNAME = "root";

    /**
     * 数据库密码
     */
    private static  String  PASSWORD = "root";

    /**
     * 数据库驱动
     */
    private static final String DRIVER = "com.mysql.jdbc.Driver";

    /**
     * 数据库类型（如mysql，oracle）
     */
    private static final String DATABASE_TYPE = "mysql";

    /**
     * @throws java.lang.Exception
     */
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        PropKit.use("a_little_config.txt");
        URL=PropKit.get("jdbcUrl");
        USERNAME=PropKit.get("user");
        PASSWORD=PropKit.get("password");
        dp=new DruidPlugin(URL, USERNAME,PASSWORD,DRIVER);

        dp.addFilter(new StatFilter());

        dp.setInitialSize(3);
        dp.setMinIdle(2);
        dp.setMaxActive(5);
        dp.setMaxWait(60000);
        dp.setTimeBetweenEvictionRunsMillis(120000);
        dp.setMinEvictableIdleTimeMillis(120000);

        WallFilter wall = new WallFilter();
        wall.setDbType(DATABASE_TYPE);
        dp.addFilter(wall);

        dp.getDataSource();
        dp.start();

        activeRecord = new ActiveRecordPlugin(dp);
        activeRecord.setDialect(new MysqlDialect())
        //.setDevMode(true)
        //.setShowSql(true)  //是否打印sql语句
        ;

        //映射数据库的表和继承与model的实体
        //只有做完该映射后，才能进行junit测试
        _MappingKit.mapping(activeRecord);
        activeRecord.start();
    }

    /**
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception {
        activeRecord.stop();
        dp.stop();
    }
}
